<?php

namespace app\admin\controller;

use think\Controller;
use think\facade\Validate;

class Login extends Controller
{
    // 登录页面
    public function login()
    {
        if (request()->isPost()) {
            $data = [
                'username' => input('username'),
                'password' => input('password'),
                'captcha' => input('captcha')
            ];
            $validate = Validate::make([
                'username|用户名' => 'require',
                'password|密码' => 'require',
                'captcha|验证码' => 'require|captcha'
            ]);
            if (!$validate->check($data)) {
                $this->result('', 0, $validate->getError(), 'json');
            } else {
                $where = [
                    'username' => $data['username'],
                    'password' => md5($data['password'])
                ];
                $userInfo = model('Admin')::where($where)->find();
                if ($userInfo) {
                    session('adminId', $userInfo['id']);
                    $this->result('', 1, '登录成功', 'json');
                } else {
                    $this->result('', 0, '用户名或者密码错误', 'json');
                }
            }
        }
        return view();
    }

    // 注销登录
    public function logout()
    {
        session(null);
        $this->result('', 1, '操作成功', 'json');
    }
}
